import pygame


class snake(object):

    def __init__(self):
        self.snake_color = pygame.Color(255, 255, 255)
        pass

    def move_step(self, not_body_list, body_list, food_list, move_dir, grid_num):
        whether_eat = 0
        whether_die = 0
        head = body_list[0].copy()
        if move_dir == 0:
            head[1] -= 1
        elif move_dir == 1:
            head[1] += 1
        elif move_dir == 2:
            head[0] -= 1
        elif move_dir == 3:
            head[0] += 1

        whether_die = self.hit_die(body_list, head, grid_num)
        if whether_die == 1:
            return not_body_list, body_list, whether_eat, whether_die

        whether_eat = self.eat_food(food_list, head)
        body_list.insert(0, head)
        not_body_list.remove(head)
        if whether_eat == 0:
            not_body_list.append(body_list[-1])
            body_list.pop()
        return not_body_list, body_list, whether_eat, whether_die

    def eat_food(self, food_list, head):
        # whether eat food
        if head == food_list:
            return 1
        else:
            return 0

    def init_body(self, grid_num):
        body_list = [[int(grid_num[0]/2), int(grid_num[1]/2)]]  # 蛇的身体头在前，列行
        not_body_list = [[i + 1, j + 1] for i in range(grid_num[0]) for j in range(grid_num[1])]
        not_body_list.remove(body_list[0])
        return body_list, not_body_list

    def draw_body(self, body_list, window, grid_size):
        for index in body_list:
            x = (index[0] - 1) * grid_size[0]
            y = (index[1] - 1) * grid_size[1]
            rec = (x, y, grid_size[0]-1, grid_size[1]-1)
            pygame.draw.rect(window, self.snake_color, rec)

    def hit_die(self, body_list, head, grid_num):
        # hit wall
        if head[0] <= 0 or head[0] > grid_num[0]:
            return 1
        if head[1] <= 0 or head[1] > grid_num[1]:
            return 1
        # hit itself
        if head in body_list:
            return 1
        return 0

    def get_s(self, body_list, food_list, grid_num):
        head = body_list[0]
        xf, xh, yf, yh, k0, k1, k2, k3 = food_list[0], head[0], food_list[1], head[1], 0, 0, 0, 0
        if [head[0]+1, head[1]] in body_list or head[0]+1 <= 0 or head[0]+1 > grid_num[0] or head[1] <= 0 or head[1] > grid_num[1]:
            k3 = 1
        if [head[0]-1, head[1]] in body_list or head[0]-1 <= 0 or head[0]-1 > grid_num[0] or head[1] <= 0 or head[1] > grid_num[1]:
            k2 = 1
        if [head[0], head[1]-1] in body_list or head[0] <= 0 or head[0] > grid_num[0] or head[1]-1 <= 0 or head[1]-1 > grid_num[1]:
            k0 = 1
        if [head[0], head[1]+1] in body_list or head[0] <= 0 or head[0] > grid_num[0] or head[1]+1 <= 0 or head[1]+1 > grid_num[1]:
            k1 = 1

        xfh = xf - xh
        yfh = yf - yh
        # fh_max = xfh + yfh
        #
        # if fh_max != 0:
        #     xfh, yfh = xfh / fh_max, yfh / fh_max
        # else:
        #     xfh, yfh = 0, 0

        return [xf, yf, xfh, yfh, k0, k1, k2, k3]
        # return [xf, xh, yf, yh, k0, k1, k2, k3]

    def get_r(self, whether_eat, whether_die, r, body_list, food_list, grid_num, d_):

        head = body_list[0]
        xf, xh, yf, yh, t = food_list[0], head[0], food_list[1], head[1], (grid_num[0]+grid_num[1])
        d = abs(xf-xh) + abs(yf-yh)

        # if whether_die == 1:
        #     r -= 20
        #     print('die', len(body_list), r)
        # else:
        #     if whether_eat == 1:
        #         r += 5
        #         print('eat', len(body_list), r)
        #     else:
        #         if d < d_:
        #             r += 4
        #         else:
        #             r -= 4
        # return r, d
        if d < d_:
            r = 1
        elif d == d_:
            r = 0
        else:
            r = -1

        if whether_die == 1:
            r -= t
        if whether_eat == 1:
            r += t

        r += len(body_list)-1

        return r, d


